<?php

require_once('smarty/SmartyInclude.php');
require_once('com/fabriciojf/include/Menu.class.php');
require_once('com/fabriciojf/include/Pagina.class.php');

// instancia um objeto smarty padrão
$smarty = SmartyInclude::getSmarty();

// permissões
$roles[] = 'admin';
$roles[] = 'usuario';
$logado = Permission::isInRole($roles);

// verifica se a página já está no cache
// pega os objetos da action
$pagina = $_REQUEST["pagina"];
$tickets = $pagina->getObjeto();

$menusuperior =
'<div class="menusuperior">' .
'    <a href="do.php?action=Home" class="menuesquerdo">Home</a>&nbsp;>' .
'    <a href="do.php?action=PainelControle">Painel de Controle</a>&nbsp;>&nbsp;Listar Tickets' .
'    <a href="do.php?action=Logout" class="menudireito">Logout</a>' .
'    <div class="clear"></div> ' .
'</div>';

$ticketlistar = '';
if (Permission::isInRole(array('usuario'))) {
$ticketlistar = 
'<form action="do.php?action=TicketCriar" method="post">' .
'    <input type="submit" class="botao" value="Novo Chamado">' .
'</form>';
}

// carrega os valores dos objetos nos vetores
foreach ($tickets as $row){
    $idTicket[] = $row->getId();
    $telefone[] = $row->getTelefone();
    $assunto[] = $row->getAssunto();
    $mensagem[] = $row->getMensagem();
    $dataHora[] = $row->getDataHora();
    $statusTicket[] = $row->getStatus()->getStatus();
    $visualizarTicket[] =
         '<form action="do.php?action=TicketAbrir" method="post">
                    <input type="hidden" name="idTicket" value="' . $row->getId() . '" />
                    <input type="submit" value="Consultar" />
                </form>';
}

$proximaPagina = $pagina->getProximaPagina();
if ($proximaPagina > -1) {
    $proximaPagina =
         '<form action="do.php?action=TicketListar" method="post">
                    <input type="hidden" name="paginaRequerida" value="' . $proximaPagina . '" />
                    <input type="submit" value="Chamados Antigos" />
                </form>';
} else {
    $proximaPagina = '';
}

$paginaAnterior = $pagina->getPaginaAnterior();
if ($paginaAnterior > 0) {
    $paginaAnterior =
         '<form action="do.php?action=TicketListar" method="post">
                    <input type="hidden" name="paginaRequerida" value="' . $paginaAnterior . '" />
                    <input type="submit" value="Chamados Recentes" />
                </form>';
} else {
    $paginaAnterior = '';
}

$smarty->assign('menuon',  Menu::getMenuOn());
$smarty->assign('menupost', Menu::getMenuPost());
$smarty->assign('menupage', Menu::getMenuPages());
$smarty->assign('menuticket', Menu::getMenuTicket());
$smarty->assign('menupostinativos', Menu::getMenuPostInativos());
$smarty->assign('menusuperior', Menu::getMenuSuperior());

$smarty->assign('paginaAnterior', $paginaAnterior);
$smarty->assign('proximaPagina', $proximaPagina);

$smarty->assign('idTicket', $idTicket);
$smarty->assign('telefone', $telefone);
$smarty->assign('assunto', $assunto);
$smarty->assign('mensagem', $mensagem);
$smarty->assign('dataHora', $dataHora);
$smarty->assign('statusTicket', $statusTicket);
$smarty->assign('visualizarTicket', $visualizarTicket);

$smarty->assign('logado', $logado);

// carrega o template
$smarty->display('TicketListar.tpl');

?>
